package com.wbb.controller;

import com.wbb.model.TbItemCat;
import com.wbb.result.JsonResult;
import com.wbb.service.ItemCatService;
import com.wbb.vo.ItemCatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/itemCat")
public class ItemCatController {
    @Autowired
    private ItemCatService itemCatService;

    @GetMapping("/selectByPid")
    public JsonResult<List<ItemCatVo>> selectByPid(Long pid){
        List<ItemCatVo> voList = itemCatService.selectByPid(pid);
        return JsonResult.ok().setData(voList);
    }

    @GetMapping("/queryAll")
    public JsonResult<List<TbItemCat>> queryAll(){
        List<Map<String, Object>> list = itemCatService.queryAll();
        return JsonResult.ok().setData(list);
    }

    @PostMapping("/save")
    public JsonResult save(@RequestBody TbItemCat itemCat){
        if (itemCat.getId() != null){
            itemCatService.updateItemCat(itemCat);
        }else {
            itemCatService.addItemCat(itemCat);
        }
        return JsonResult.ok();
    }

    @DeleteMapping("/deleteById/{id}")
    public JsonResult deleteById(@PathVariable Long id){
        itemCatService.deleteById(id);
        return JsonResult.ok();
    }

    @PatchMapping("/batchItemCat")
    public JsonResult batchItemCat(List<Long> ids){
        itemCatService.batchItemCat(ids);
        return JsonResult.ok();
    }
}
